using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.Data.SqlClient;

namespace OrderSystem.DataAccess
{
	public class Customers : IDisposable
	{
		private SqlDataAdapter dataAdapter;

		private SqlCommand selectCommand;

		private SqlCommand insertCommand;

		private SqlCommand updateCommand;

		private SqlCommand deleteCommand;

		public DataTable GetCustomers(string strQuery)
		{
			DataTable dataTable = new DataTable("Customer");
			try
			{
				this.selectCommand = new SqlCommand(strQuery, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public bool InsertCustomer(string sCompanyName, string sLeader, string sLeaderPhone, string sCompanyPhone, string sFax, string sCompanyAddress, string sLinkman, string sLinkmanPhone, string sEmail, string sType, bool bAddProject)
		{
			try
			{
				if (bAddProject)
				{
					this.insertCommand = new SqlCommand("SET XACT_ABORT ON BEGIN TRANSACTION INSERT INTO Customer VALUES(@CompanyName,@Leader,@LeaderPhone,@CompanyPhone,@Fax,@CompanyAddress,@Linkman,@LinkmanPhone,@Email,@Type) INSERT INTO Project VALUES(@@identity,@CompanyName,@Linkman,@LinkmanPhone,@CompanyAddress,0,'') COMMIT TRANSACTION", new SqlConnection(AppSettings.ConnectionString));
				}
				else
				{
					this.insertCommand = new SqlCommand("INSERT INTO Customer VALUES(@CompanyName,@Leader,@LeaderPhone,@CompanyPhone,@Fax,@CompanyAddress,@Linkman,@LinkmanPhone,@Email,@Type)", new SqlConnection(AppSettings.ConnectionString));
				}
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@CompanyName"].Value = sCompanyName;
				this.insertCommand.Parameters.Add(new SqlParameter("@Leader", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters["@Leader"].Value = sLeader;
				this.insertCommand.Parameters.Add(new SqlParameter("@LeaderPhone", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@LeaderPhone"].Value = sLeaderPhone;
				this.insertCommand.Parameters.Add(new SqlParameter("@CompanyPhone", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@CompanyPhone"].Value = sCompanyPhone;
				this.insertCommand.Parameters.Add(new SqlParameter("@Fax", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@Fax"].Value = sFax;
				this.insertCommand.Parameters.Add(new SqlParameter("@CompanyAddress", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters["@CompanyAddress"].Value = sCompanyAddress;
				this.insertCommand.Parameters.Add(new SqlParameter("@Linkman", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters["@Linkman"].Value = sLinkman;
				this.insertCommand.Parameters.Add(new SqlParameter("@LinkmanPhone", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@LinkmanPhone"].Value = sLinkmanPhone;
				this.insertCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters["@Email"].Value = sEmail;
				this.insertCommand.Parameters.Add(new SqlParameter("@Type", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters["@Type"].Value = sType;
				this.insertCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public bool UpdateCustomer(int nCustomerID, string sCompanyName, string sLeader, string sLeaderPhone, string sCompanyPhone, string sFax, string sCompanyAddress, string sLinkman, string sLinkmanPhone, string sEmail, string sType)
		{
			try
			{
				this.updateCommand = new SqlCommand("UPDATE Customer SET CompanyName=@CompanyName,Leader=@Leader,LeaderPhone=@LeaderPhone,CompanyPhone=@CompanyPhone,Fax=@Fax,CompanyAddress=@CompanyAddress,Linkman=@Linkman,LinkmanPhone=@LinkmanPhone,Email=@Email,Type=@Type WHERE PKID=@CustomerID", new SqlConnection(AppSettings.ConnectionString));
				this.updateCommand.Connection.Open();
				this.updateCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.updateCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.updateCommand.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@CompanyName"].Value = sCompanyName;
				this.updateCommand.Parameters.Add(new SqlParameter("@Leader", SqlDbType.NVarChar, 16));
				this.updateCommand.Parameters["@Leader"].Value = sLeader;
				this.updateCommand.Parameters.Add(new SqlParameter("@LeaderPhone", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@LeaderPhone"].Value = sLeaderPhone;
				this.updateCommand.Parameters.Add(new SqlParameter("@CompanyPhone", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@CompanyPhone"].Value = sCompanyPhone;
				this.updateCommand.Parameters.Add(new SqlParameter("@Fax", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@Fax"].Value = sFax;
				this.updateCommand.Parameters.Add(new SqlParameter("@CompanyAddress", SqlDbType.NVarChar, 64));
				this.updateCommand.Parameters["@CompanyAddress"].Value = sCompanyAddress;
				this.updateCommand.Parameters.Add(new SqlParameter("@Linkman", SqlDbType.NVarChar, 16));
				this.updateCommand.Parameters["@Linkman"].Value = sLinkman;
				this.updateCommand.Parameters.Add(new SqlParameter("@LinkmanPhone", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@LinkmanPhone"].Value = sLinkmanPhone;
				this.updateCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar, 32));
				this.updateCommand.Parameters["@Email"].Value = sEmail;
				this.updateCommand.Parameters.Add(new SqlParameter("@Type", SqlDbType.NVarChar, 16));
				this.updateCommand.Parameters["@Type"].Value = sType;
				this.updateCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public bool Delete(int nCustomerID)
		{
			try
			{
				this.deleteCommand = new SqlCommand("Delete from Customer where PKID=@CustomerID", new SqlConnection(AppSettings.ConnectionString));
				this.deleteCommand.Connection.Open();
				this.deleteCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.deleteCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.deleteCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(true);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.dataAdapter != null)
				{
					if (this.dataAdapter.SelectCommand != null)
					{
						if (this.dataAdapter.SelectCommand.Connection != null)
						{
							this.dataAdapter.SelectCommand.Connection.Dispose();
							this.dataAdapter.SelectCommand.Connection = null;
						}
						this.dataAdapter.SelectCommand.Dispose();
						this.dataAdapter.SelectCommand = null;
					}
					this.dataAdapter.Dispose();
					this.dataAdapter = null;
				}
				if (this.insertCommand != null)
				{
					if (this.insertCommand.Connection != null)
					{
						this.insertCommand.Connection.Dispose();
						this.insertCommand.Connection = null;
					}
					this.insertCommand.Dispose();
					this.insertCommand = null;
				}
				if (this.updateCommand != null)
				{
					if (this.updateCommand.Connection != null)
					{
						this.updateCommand.Connection.Dispose();
						this.updateCommand.Connection = null;
					}
					this.updateCommand.Dispose();
					this.updateCommand = null;
				}
				if (this.deleteCommand != null)
				{
					if (this.deleteCommand.Connection != null)
					{
						this.deleteCommand.Connection.Dispose();
						this.deleteCommand.Connection = null;
					}
					this.deleteCommand.Dispose();
					this.deleteCommand = null;
				}
			}
		}
	}
}
